package jp.ac.u_tokyo.dch.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
/**
* @version 1.1
*/
public class TMDBConnection {
	 /** ロガー */
	private static DchLogger logger =  DchLogger.getLogger(TMDBConnection.class);
	/**
	 * DBコネクションオブジェクトを取得する
	 * @return  DBコネクションオブジェクト
	 * @throws DBException 例外
	 */
	public static DBConnection getTmDbconnection() throws DBException {
		DBConnection dbConn = null;
		try {
			Context initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			DataSource ds = (DataSource) envCtx.lookup("jdbc/tmdb");
			Connection conn = ds.getConnection();
			dbConn = new DBConnection(conn);
			dbConn.setAutoCommit(false);
		} catch (NamingException e1) {
			logger.error(e1);
			throw new DBException(e1);
		} catch (SQLException e) {
			logger.error(e);
			throw new DBException(e);
		} catch (DBException e) {
			logger.error(e);
			throw new DBException(e);
		}

		return dbConn;
	}
}
